home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Libraries / Effect library / Demo ƒ / Wipes ƒ / Hilbert wipe.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-01-09  |  4.1 KB  |  143 lines  |  [TEXT/KAHL]

  1. /**********************************************************************\
  2.  
  3. File:        Hilbert wipe.c
  4.  
  5. Purpose:    Graphic effect from offscreen bitmap to main window (on
  6.             screen).  See comments below for more description.
  7.  
  8.  
  9. MSG Demo -- graphic effects demonstration program
  10. Copyright (C) 1992-4 Mark Pilgrim & Dave Blumenthal
  11.  
  12. This program is free software; you can redistribute it and/or modify
  13. it under the terms of the GNU General Public License as published by
  14. the Free Software Foundation; either version 2 of the License, or
  15. (at your option) any later version.
  16.  
  17. This program is distributed in the hope that it will be useful,
  18. but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20. GNU General Public License for more details.
  21.  
  22. You should have received a copy of the GNU General Public License
  23. along with this program in a file named "GNU General Public License".
  24. If not, write to the Free Software Foundation, 675 Mass Ave,
  25. Cambridge, MA 02139, USA.
  26.  
  27. \**********************************************************************/
  28.  
  29. #include "msg timing.h"
  30.  
  31. /* This fills the screen by a Hilbert space-filling curve, which is defined by
  32.    two mutually recursive drawing functions:
  33.    
  34.    X = left, Y, draw, right, X, draw, X, right, draw, Y, left
  35.    Y = right, X, draw, left, Y, draw, Y, left, draw, X, right
  36.    
  37.    Start by drawing X at the highest recursion level from the bottomleft of the
  38.    screen. (At recursion level 1, X and Y are null functions.)
  39. */
  40.  
  41. #define    RecursionLevel    4
  42. #define CorrectTime 1
  43. #define theWindowHeight (boundsRect.bottom-boundsRect.top)
  44. #define theWindowWidth (boundsRect.right-boundsRect.left)
  45.  
  46. pascal void HilbertRecurse(GrafPtr sourceGrafPtr, GrafPtr destGrafPtr, Rect boundsRect,
  47.     int level, int whichpattern, int* x, int* y);
  48. pascal short HilbertWipe(GrafPtr sourceGrafPtr, GrafPtr destGrafPtr, Rect boundsRect);
  49.  
  50. typedef char    Hilby[11];
  51.  
  52. static Hilby    HilbertPattern[]=
  53. {
  54.     {
  55.         0x02,0x69,0x00,0x01,0x42,0x00,0x42,0x01,0x00,0x69,0x02
  56.     },
  57.     {
  58.         0x01,0x42,0x00,0x02,0x69,0x00,0x69,0x02,0x00,0x42,0x01
  59.     }
  60. };
  61.  
  62. static int            HilbertDirection;
  63. static int            vBlockSize;
  64. static int            hBlockSize;
  65. static RgnHandle    boundsRgn;
  66.  
  67. pascal void HilbertRecurse(GrafPtr sourceGrafPtr, GrafPtr destGrafPtr, Rect boundsRect,
  68.     int level, int whichpattern, int* x, int* y)
  69. {
  70.     int                i;
  71.     Rect            source;
  72.     
  73.     for (i=0; i<11; i++)
  74.     {
  75.         switch (HilbertPattern[whichpattern][i])
  76.         {
  77.             case 0x01:     /* turn left */
  78.                 HilbertDirection--;
  79.                 if (HilbertDirection<0) HilbertDirection=3;
  80.                 break;
  81.             case 0x02:     /* turn right */
  82.                 HilbertDirection++;
  83.                 if (HilbertDirection==4) HilbertDirection=0;
  84.                 break;
  85.             case 0x00:    /* draw */
  86.                 StartTiming();
  87.                 SetRect(&source, *x, *y-vBlockSize, *x+hBlockSize, *y);
  88.                 OffsetRect(&source, boundsRect.left, boundsRect.top);
  89.                 CopyBits(&(sourceGrafPtr->portBits), &(destGrafPtr->portBits),
  90.                                 &source, &source, 0, boundsRgn);
  91.                 switch (HilbertDirection)
  92.                 {
  93.                     case 0:
  94.                         *x+=hBlockSize;
  95.                         break;
  96.                     case 1:
  97.                         *y-=vBlockSize;
  98.                         break;
  99.                     case 2:
  100.                         *x-=hBlockSize;
  101.                         break;
  102.                     case 3:
  103.                         *y+=vBlockSize;
  104.                         break;
  105.                 }
  106.                 TimeCorrection(CorrectTime);
  107.                 break;
  108.             case 0x42:    /* call X */
  109.                 if (level>1)
  110.                     HilbertRecurse(sourceGrafPtr,destGrafPtr,boundsRect,level-1,0,x,y);
  111.                 break;
  112.             case 0x69:    /* call Y */
  113.                 if (level>1)
  114.                     HilbertRecurse(sourceGrafPtr,destGrafPtr,boundsRect,level-1,1,x,y);
  115.                 break;
  116.         }
  117.     }
  118. }
  119.  
  120. pascal short HilbertWipe(GrafPtr sourceGrafPtr, GrafPtr destGrafPtr, Rect boundsRect)
  121. {
  122.     int            curx, cury;
  123.     int            answer, i;
  124.     
  125.     answer=1;
  126.     for (i=0; i<RecursionLevel; i++)
  127.         answer*=2;
  128.     vBlockSize=1+theWindowHeight/answer;    /* used to be 20 */
  129.     hBlockSize=1+theWindowWidth/answer;        /* used to be 32 */
  130.     HilbertDirection=0;
  131.     boundsRgn=NewRgn();
  132.     SetRectRgn(boundsRgn, boundsRect.left, boundsRect.top, boundsRect.right, boundsRect.bottom);
  133.     cury=theWindowHeight;
  134.     curx=0;
  135.     HilbertRecurse(sourceGrafPtr,destGrafPtr,boundsRect,RecursionLevel,0,&curx,&cury);
  136.     CopyBits(&(sourceGrafPtr->portBits), &(destGrafPtr->portBits),
  137.                 &boundsRect, &boundsRect, 0, 0L);  /* in case we missed any bits */
  138.     
  139.     DisposeRgn(boundsRgn);
  140.     
  141.     return 0;
  142. }
  143.